<?php

namespace app\admin\model\wanlshop;

use think\Model;
use traits\model\SoftDelete;

class Package extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'wanlshop_package';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'delivery_mode_text',
        'idcard_status_text',
        'delivery_status_text',
        'delivertime_text',
        'status_text'
    ];
    

    
    public function getDeliveryModeList()
    {
        return ['created' => __('Delivery_mode created'), 'self' => __('Delivery_mode self'), 'instead' => __('Delivery_mode instead')];
    }

    public function getIdcardStatusList()
    {
        return ['0' => __('Idcard_status 0'), '1' => __('Idcard_status 1')];
    }

    public function getDeliveryStatusList()
    {
        return ['0' => __('Delivery_status 0'), '1' => __('Delivery_status 1')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }


    public function getDeliveryModeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delivery_mode']) ? $data['delivery_mode'] : '');
        $list = $this->getDeliveryModeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIdcardStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
        $list = $this->getIdcardStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDeliveryStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delivery_status']) ? $data['delivery_status'] : '');
        $list = $this->getDeliveryStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDelivertimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delivertime']) ? $data['delivertime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setDelivertimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function wanlshoporder()
    {
        return $this->belongsTo('Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goods()
    {
        return $this->hasMany('Packagegoods', 'package_id', 'id');
    }


    public function wanlshopshop()
    {
        return $this->belongsTo('Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
